<?php
namespace app\controller\admin;

use app\services\admin\PermissionService;
use app\validate\admin\PermissionValidate;
use think\App;
use think\Request;
use think\Validate;

class Permission extends Application
{
    protected $permissionService;

    public function __construct(App $app = null, PermissionService $permissionService)
    {
        parent::__construct($app);
        $this->permissionService = $permissionService;
    }

    public function index()
    {
        return view('admin/permission/index');
    }

    public function list(Request $request)
    {
        $status = $request->get('status', 0);
        $search = urldecode($request->get('search', ''));

       return view('admin/permission/list', [
            'page_title' => '权限列表',
            'meta' => $this->permissionService->getPermissionList($status, $search),
        ]);
    }

    public function edit(Request $request)
    {
        $uid = $request->param('id');
        if ($request->isPost() && $request->isAjax()) {
            $data = $request->post();
            $result = $this->validate($data, PermissionValidate::class);

            if (true !== $result) {
                return $this->formatResult(-1, $result, [
                    'token' => $request->token('_token')
                ]);
            }

            if ($uid) {
                list($ret, $msg) = $this->permissionService->modify($data);
            } else {
                list($ret, $msg) = $this->permissionService->save($data);
            }
            if ($ret) {
                return $this->formatResult(0, $msg, ['url' => url('admin/permission/list')]);
            }
            return $this->formatResult(1, $msg, [
                'token' => $request->token('_token')
            ]);
        }

        $meta['permission'] = $this->permissionService->getPermission($uid);
        $meta['groups'] = $this->permissionService->getGroupsToSelect();

        return view('admin/permission/edit', [
            'page_title' => $uid ? '编辑权限' : '新增权限',
            'meta' => $meta,
        ]);
    }

    public function del(Request $request)
    {
        $id = $request->get('id', '');

        if ($id) {
            $res = $this->permissionService->del($id);
            if ($res) {
                return $this->formatResult(0, '删除成功');
            }
        }
        return $this->formatResult(1, '删除失败');
    }

    public function groupList(Request $request)
    {
        $status = $request->get('status', 0);
        $search = urldecode($request->get('search', ''));

        return view('admin/permission/group_list', [
            'page_title' => '分组列表',
            'meta' => $this->permissionService->getPermissionGroupList($status, $search),
        ]);
    }

    public function groupEdit(Request $request)
    {
        $uid = $request->param('id');
        if ($request->isPost() && $request->isAjax()) {
            $data = $request->post();
            $result = $this->validate($data, PermissionValidate::class . '.group');

            if (true !== $result) {
                return $this->formatResult(-1, $result, [
                    'token' => $request->token('_token')
                ]);
            }

            if ($uid) {
                list($ret, $msg) = $this->permissionService->groupModify($data);
            } else {
                list($ret, $msg) = $this->permissionService->groupSave($data);
            }
            if ($ret) {
                return $this->formatResult(0, $msg, ['url' => url('admin/permission/group')]);
            }
            return $this->formatResult(1, $msg, [
                'token' => $request->token('_token')
            ]);
        }

        return view('admin/permission/group_edit', [
            'page_title' => $uid ? '编辑分组' : '新增分组',
            'meta' => $this->permissionService->getPermissionGroup($uid),
        ]);
    }

    public function groupDel(Request $request)
    {
        $id = $request->get('id', '');

        if ($id) {
            $res = $this->permissionService->groupDel($id);
            if ($res) {
                return $this->formatResult(0, '删除成功');
            }
        }
        return $this->formatResult(1, '删除失败');
    }
}
